package com.cloud.business.controller;

import cn.hutool.json.JSON;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.business.dto.SaveDTO;
import com.cloud.business.dto.SelectDTO;
import com.cloud.business.entity.DemoServerTable;
import com.cloud.business.feign.DemoClientFeign;
import com.cloud.business.service.DemoService;
import com.cloud.business.vo.ResultVO;
import com.cloud.business.vo.TableResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 测试demo
 */
@Api(tags = {"测试接口-server"})
@Slf4j
@RestController
@Validated
@RequestMapping(value = "/demo/server")
public class DemoController extends BaseController{

    @Autowired
    private DemoService demoService;
    @Autowired
    private DemoClientFeign demoClientFeign;

    @ApiOperation("测试校验的接口")
    @PostMapping("/list")
    public TableResultVO test(@RequestBody SelectDTO dto) {
        Page<DemoServerTable> list = demoService.selectList(dto);
        Integer count = demoClientFeign.feign();
        log.info("远程调用结果="+ JSONUtil.toJsonStr(count));
        return TableResultVO.success(list);
    }

    @ApiOperation("测试校验的接口1")
    @PostMapping("/list1")
    public TableResultVO test1(@RequestBody SelectDTO dto) {
        Page<DemoServerTable> list = demoService.selectList(dto);
        TableResultVO clients = demoClientFeign.test(dto);
        log.info("远程调用结果="+ JSONUtil.toJsonStr(clients));
        return TableResultVO.success(list);
    }

    @ApiOperation("保存接口")
    @PostMapping("/save")
    public ResultVO save(@RequestBody SaveDTO saveDTO) {
        return toAjax(demoService.save(saveDTO));
    }

    @ApiOperation("分布式保存接口")
    @PostMapping("/saveSeata")
    public ResultVO saveSeata(@RequestBody SaveDTO saveDTO) {
        return toAjax(demoService.saveSeata(saveDTO));
    }
}
